<!--
 * @description 福利短信提醒列表
 * @author dumeng
 * @date 2022/2/7
 -->
<template>
  <div class="sms-tip-page">
    <el-collapse-transition>
      <section v-show="searchshow" class="main-search merge">
        <span class="svg-container">
          <svg-icon icon-class="search" />
        </span>
        <el-input
          v-model.trim="listQuery.userName"
          placeholder="输入姓名搜索"
          class="search-input"
          @keyup.enter.native="handleFilter"
        />
        <section class="search-btn">
          <el-button class="filter-item" type="primary" @click="handleFilter">搜索</el-button>
          <el-button type="primary" @click="handleReset">重置</el-button>
          <span class="search-more" @click="searchshow = !searchshow">
            更多筛选条件
            <i class="el-icon-arrow-down" />
          </span>
        </section>
      </section>
    </el-collapse-transition>
    <el-collapse-transition>
      <section v-show="!searchshow" class="main-search separate">
        <section class="search-inner">
          <el-row>
            <el-col :span="8">
              <section class="search-inner-item">
                <div class="item-label">姓名</div>
                <el-input
                  v-model.trim="search.userName"
                  style="width: 90%;"
                  class="filter-item"
                  placeholder="请输入姓名搜索"
                />
              </section>
            </el-col>
            <el-col :span="8">
              <section class="search-inner-item">
                <div class="item-label">手机号码</div>
                <el-input
                  v-model.trim="search.phoneNumber"
                  style="width: 90%;"
                  class="filter-item"
                  placeholder="请输入手机号码搜索"
                />
              </section>
            </el-col>
            <el-col :span="8">
              <section class="search-inner-item">
                <div class="item-label">身份证号</div>
                <el-input
                  v-model.trim="search.userIdnumber"
                  style="width: 90%;"
                  class="filter-item"
                  placeholder="请输入身份证号搜索"
                />
              </section>
            </el-col>
          </el-row>
        </section>
        <section class="search-btn">
          <el-button type="primary" @click="handleFilter">搜索</el-button>
          <el-button type="primary" @click="handleReset">重置</el-button>
          <span class="search-more" @click="searchshow = !searchshow">
            精简筛选条件
            <i class="el-icon-arrow-up" />
          </span>
        </section>
      </section>
    </el-collapse-transition>
    <section class="main-buttons">
      <section class="main-buttons-title">
        <span>福利短信提醒</span>
      </section>
      <section class="main-buttons-btns">
        <el-button class="filter-item" style="margin-left: 10px;" @click="handleOpenTip">发送提醒</el-button>
        <el-button style="padding: 10px 30px" @click="handleCancel">返回</el-button>
      </section>
    </section>
    <section class="main-table">
      <v-table
        :list="list"
        tag="derectionalSmsTip"
        :list-loading="listLoading"
        :filter-column="filtercolumn"
        :selection="false"
      />
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
      />
    </section>
    <!-- 短信提醒弹窗 -->
    <el-dialog
      v-el-drag-dialog
      :title="actName"
      :show-close="false"
      :visible.sync="smsVisible"
      custom-class="sms-tip-dialog"
      width="550px"
      :destroy-on-close="true"
      :close-on-click-modal="false"
    >
      <div class="sms-container">
        <el-checkbox-group v-model="sendType">
          <el-row style="font-size: 14px;">
            <el-col :span="12">未领取人数：<span class="red">1</span>人</el-col>
            <el-col :span="12" class="check-col"><el-checkbox label="1">短信提醒</el-checkbox></el-col>
            <el-col :span="12">已领取且有余额人数：<span class="red">1</span>人</el-col>
            <el-col :span="12" class="check-col"><el-checkbox label="2">短信提醒</el-checkbox></el-col>
          </el-row>
        </el-checkbox-group>
      </div>
      <span slot="footer">
        <el-button @click="smsVisible = false,sendType=[]">取 消</el-button>
        <el-button style="background-color: #EB523C;color:#fff;" @click="handleSendSms">发送短信提醒</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import VTable from '@/components/Table'
import { queryPage, get, sendTip } from '../api/smsTip'
import CryptoJS from '@/utils/aesJiaJM'
import elDragDialog from '@/directive/el-drag-dialog'

export default {
  name: 'ActivitySmsTip',
  components: { Pagination, VTable },
  directives: { elDragDialog },
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 10,
        userName: ''
      },
      searchshow: true,
      search: {
        userName: '',
        phoneNumber: '',
        userIdnumber: ''
      },
      list: [],
      total: 0,
      listLoading: false,
      id: '',
      actName: '',
      smsVisible: false,
      sendType: []
    }
  },
  created() {
    this.id = this.$route.query.id
    this.actName = this.$route.query.n
    this.getList()
  },
  methods: {
    handleCancel() {
      this.$router.push({ path: this.$filterPath('index'), query: { t: this.$route.query.t }})
    },
    filtercolumn(scope, item) {
      if (item === 'phoneNumber' || item === 'userIdnumber') {
        return CryptoJS.$aesJieM(scope.row[item])
      } else {
        return scope.row[item]
      }
    },

    handleReset() {
      this.listQuery.page = 1
      this.listQuery.userName = ''
      for (const i in this.search) {
        if (this.search['' + i + ''] instanceof Array) {
          this.search['' + i + ''] = []
        } else {
          this.search['' + i + ''] = ''
        }
      }
      this.getList()
    },

    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },

    getList(obj) {
      this.listLoading = true
      var query = {
        page: obj ? obj.page : this.listQuery.page,
        pageSize: obj ? obj.limit : this.listQuery.limit,
        giveId: this.id
      }
      if (this.searchshow) {
        query['userName'] = this.listQuery.userName
      } else {
        query['userName'] = this.search.userName
        query['phoneNumber'] = this.search.phoneNumber
        query['userIdnumber'] = this.search.userIdnumber
      }
      return new Promise((resolve, reject) => {
        // todo 接口没写
        queryPage(query).then(response => {
          this.list = response.data.data.rows
          this.total = response.data.data.total
          this.listLoading = false
        }).catch(error => {
          reject(error)
          this.listLoading = false
        })
      })
    },
    /**
     * 打开短信提醒弹窗
     */
    handleOpenTip() {
      // todo 调查询发放人数详情接口后再打开
      this.smsVisible = true
      this.sendType = []
    },
    /**
     * 发送短信提醒
     */
    handleSendSms() {
      this.$confirm('确认发送短信提醒?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({
          lock: true,
          text: '请稍后',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.3)'
        })
        return new Promise((resolve, reject) => {
          sendTip({ id: this.id, sendType: this.sendType.join(',') }).then(() => {
            loading.close()
            this.$message({
              title: '成功',
              message: '发送成功',
              type: 'success',
              duration: 2000
            })
            this.smsVisible = false
            this.getList()
          }).catch(error => {
            loading.close()
            reject(error)
          })
        })
      }).catch(() => {

      })
    }

  }
}
</script>

<style lang="scss">
@import '@/styles/mixin.scss';

.sms-tip-page {
  padding: 0 10px 10px;

  .main-search {
    background-color: #fff;
    padding: 0 10px;

    .svg-container {
      padding: 0 5px 0 10px;
      color: #bfced6;
      vertical-align: middle;
      display: inline-block;
    }

    .search-input {
      width: 60%;
      position: relative;

      &:after {
        content: '';
        position: absolute;
        left: -25px;
        right: 0;
        bottom: 8px;
        height: 1px;
        background: rgba(226, 227, 232, 1);
      }

      input {
        outline: none;
        background: transparent;
        border: none;
        outline: medium;
        padding: 0;

        &:focus {
          outline: none;
          background-color: transparent;
        }
      }
    }

    .search-btn {
      display: inline-block;
      float: right;

      .el-button.is-round {
        padding: 10px 30px;
        margin-right: 34px;
      }

      .search-more {
        font-size: 14px;
        font-weight: 500;
        @include font-color($mainColor);
        cursor: pointer;
        margin-left: 10px;
      }
    }

    &.merge {
      height: 50px;
      line-height: 50px;
    }

    &.separate {
      .search-inner {
        padding-top: 10px;

        .search-inner-item {
          .item-label {
            height: 30px;
            line-height: 30px;
            font-size: 14px;
            color: #a6a6a6;
            font-weight: 400;
          }

          .el-input {
            position: relative;

            &.el-date-editor input {
              padding-left: 30px;
            }

            .el-input__inner {
              border: none;
              padding: 0;
            }

            &:after {
              content: '';
              height: 1px;
              background-color: #e2e3e8;
              position: absolute;
              left: 0;
              right: 0;
              bottom: 0;
            }
          }
        }
      }

      .search-btn {
        text-align: center;
        display: block;
        float: none;
        height: 56px;
        line-height: 56px;
      }
    }
  }

  .main-buttons {
    display: flex;
    line-height: 36px;
    margin-top: 10px;

    .main-buttons-title {
      flex: 1;
      height: 36px;
      line-height: 36px;
      font-size: 16px;
      font-weight: bold;
      color: rgba(51, 51, 51, 1);
    }
  }
  .sms-tip-dialog {
    .sms-container {
      .el-col-12 {
        margin-top: 20px;
        font-weight: bold;
        padding-left: 20px;
        .red {
          color: red;
        }
        &.check-col {
          text-align: center;
          padding-left: 0;
        }
      }
    }
    .el-dialog__footer{
      text-align: center !important;
      margin-top: 10px;
    }
  }
}
</style>
